package org.example.petitionplatformsystem.controller;

import org.example.petitionplatformsystem.Response;
import org.example.petitionplatformsystem.converter.PetitionConverter;
import org.example.petitionplatformsystem.dao.model.PetitionEvents;
import org.example.petitionplatformsystem.dto.PetitionDTO.PetitionPostDTO;
import org.example.petitionplatformsystem.service.EmotionAnalysisService;
import org.example.petitionplatformsystem.service.PetitionEventsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consulting")
public class PetitionEventsController {
    //信访事件表的服务
    private final PetitionEventsService petitionEventsService;
    @Autowired
    private EmotionAnalysisService emotionAnalysisService;
    @Autowired
    public PetitionEventsController(PetitionEventsService petitionEventsService){
        this.petitionEventsService = petitionEventsService;
    }

    //用户获取信访记录
    @GetMapping("/{userId}/getConsultRecord")
    public Response<List<PetitionEvents>> getConsultRecord(@PathVariable("userId") long userid){

        return Response.newSuccessResponse(petitionEventsService.getAllPetitionEventsByUserId(userid));
    }
    //获取某个用户的某个信访记录
    @GetMapping("/{userid}/getConsultRecord/{petitionid}")
    public Response<PetitionEvents> getConsultRecord(@PathVariable("userid") long userid,@PathVariable("petitionid") long petitionid){

        return Response.newSuccessResponse(petitionEventsService.getPetitionEventByPetitionIdAndUserid(petitionid,userid));
    }
    //业务员写入信访记录
    @PostMapping("/recordConsultation")
    public Response<PetitionEvents> recordConsultation(@RequestBody PetitionEvents petitionEvents){

        LocalDateTime now = LocalDateTime.now();
        petitionEvents.setCreatedAt(String.valueOf(now));
        System.out.println(petitionEvents.getCreatedAt());
        System.out.println("Userid"+petitionEvents.getUserID());
        System.out.println(petitionEvents.getKeywords());
        String title = petitionEvents.getTitle();
        // 运行Python脚本并传递参数
        String pythonScriptPath = "src/main/python/sentiment_analysis.py"; // 修改为实际路径
        double sentimentScore = emotionAnalysisService.getSentimentScoreFromPython(pythonScriptPath, title);

        // 设置情感得分
        petitionEvents.setSentiment(sentimentScore);

        return Response.newSuccessResponse(petitionEventsService.addPetitionEvent(petitionEvents));

    }

    //用户在线提交信访信息
    @PostMapping("/inLine")
    public Response<PetitionEvents> inLine(@RequestBody PetitionPostDTO petitionEvents){
        System.out.println("title "+petitionEvents.getInstitution());
        PetitionEvents petitionEvent = PetitionConverter.toPetitionEvents(petitionEvents);
        System.out.println(petitionEvent.getPetitionDep());
        return Response.newSuccessResponse(petitionEventsService.addPetitionEvent(petitionEvent));
    }
    //用户对信访记录进行满意度评价
    @PostMapping("/postSatisfaction&petitionid={petitionId}&degree={degree}")
    public Response<PetitionEvents> postSatisfaction(@PathVariable long petitionId,@PathVariable double degree){
        System.out.println("petitionid "+petitionId);
        System.out.println("degree "+degree);
        PetitionEvents petitionEvents = petitionEventsService.getPetitionEventById(petitionId);
        petitionEvents.setDegree(degree);

        return Response.newSuccessResponse(petitionEventsService.updatePetitionEvent(petitionEvents));
    }



}
